package cn.fhkj.web.controller;

import cn.fhkj.config.ProjectProperties;
import com.github.pagehelper.PageInfo;
import cn.fhkj.bean.ResultBean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Slf4j
public class BaseController {
  public static final int PAGESIZE = 10;
  protected static final String ERROR_NO_USER = "无效用户";

  @Autowired protected HttpServletRequest request;

  @Autowired protected HttpServletResponse response;

  @Autowired private ProjectProperties projectProperties;

  /**
   * 处理资源，能够通过url访问。 本项目的web和manage，图片保存在指定文件夹内，web通过指定的配置urlpath，来获得访问url。 如果资源已经是一个完整url，那么不进行处理。
   *
   * @param resourcePath
   * @return
   */
  protected final String handleResToUrl(String resourcePath) {
    if (resourcePath != null
        && !resourcePath.trim().equals("")
        && !resourcePath.startsWith("http")) {
      String resUrl = projectProperties.getUrlpath() + resourcePath;
      log.debug("原资源路径[{}]更改后的存储url为[{}]", resourcePath, resUrl);
      return resUrl;
    } else {
      return resourcePath;
    }
  }

  /**
   * 设置页数
   *
   * @param resultBean
   * @param list
   * @param <T>
   */
  protected <T> void countPage(ResultBean resultBean, List<T> list) {
    PageInfo<T> pageInfo = new PageInfo<>(list);

    resultBean.setTotalPage(pageInfo.getPages());
    resultBean.setTotal(pageInfo.getTotal());
    resultBean.setPage(pageInfo.getPageNum());
    resultBean.setPageSize(pageInfo.getPageSize());

  }


}
